//
//  ACNetworking.m
//  ACFramework
//
//  Created by caoyq on 2018/11/12.
//

#import "ACNetworking.h"

@implementation ACNetworking

#pragma mark - POST

+ (NSURLSessionTask *)postWithUrl: (NSString *)url
                           vcName: (NSString *)vcName
                  allHeaderFields: (NSDictionary *)allHeaderFields
                       parameters: (NSDictionary *)parameters
                          success: (ACResponseSuccessBlock)success
                          failure: (ACResponseFailureBlock)failure
{
    return [self requestWithUrl:url methodType:ACMethodTypePost requestType:ACRequestTypeJSON vcName:vcName fileSavePath:nil allHeaderFields:allHeaderFields parameters:parameters downloadProgress:nil success:success failure:failure];
}

#pragma mark - GET

+ (NSURLSessionTask *)getWithUrl: (NSString *)url
                          vcName: (NSString *)vcName
                 allHeaderFields: (NSDictionary *)allHeaderFields
                      parameters: (NSDictionary *)parameters
                         success: (ACResponseSuccessBlock)success
                         failure: (ACResponseFailureBlock)failure
{
    return [self requestWithUrl:url methodType:ACMethodTypeGet requestType:ACRequestTypeJSON vcName:vcName fileSavePath:nil allHeaderFields:allHeaderFields parameters:parameters downloadProgress:nil success:success failure:failure];
}

#pragma mark - download

+ (NSURLSessionTask *)downloadWithUrl: (NSString *)url
                               vcName: (NSString *)vcName
                         fileSavePath: (NSString *)fileSavePath
                      allHeaderFields: (NSDictionary *)allHeaderFields
                     downloadProgress: (ACResponseProgressBlock)downloadProgress
                              success: (ACResponseSuccessBlock)success
                              failure: (ACResponseFailureBlock)failure
{
    return [self requestWithUrl:url methodType:ACMethodTypeDownload requestType:ACRequestTypePlainText vcName:vcName fileSavePath:fileSavePath allHeaderFields:allHeaderFields parameters:nil downloadProgress:downloadProgress success:success failure:failure];
}

#pragma mark - upload

+ (NSURLSessionTask *)uploadWithUrl: (NSString *)url
                             vcName: (NSString *)vcName
                          parameter: (NSDictionary *)parameter
                         uploadType: (ACUploadType)uploadType
                           fileData: (NSData *)fileData
                            fileURL: (NSURL *)fileURL
                           fileName: (NSString *)fileName
                           mimeType: (NSString *)mimeType
                    allHeaderFields: (NSDictionary *)allHeaderFields
                     uploadProgress: (ACResponseProgressBlock)uploadProgress
                            success: (ACResponseSuccessBlock)success
                            failure: (ACResponseFailureBlock)failure
{
    ACRequestBody *requestBody = [[ACRequestBody alloc] init];
    requestBody.methodType = ACMethodTypeUpload;
    requestBody.requestType = ACRequestTypePlainText;
    requestBody.url = url;
    requestBody.allHeaderFields = allHeaderFields;
    requestBody.parameters = parameter;
    
    if (!fileName.length) { //针对没有名字的情况
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyyMMddHHmmss";
        NSString *name = [formatter stringFromDate:[NSDate date]];
        NSString *suffix = uploadType == ACUploadTypeImage ? @".png" : @".mov";
        fileName = [name stringByAppendingString:suffix];
    }
    if (!mimeType.length) {
        mimeType = uploadType == ACUploadTypeImage ? @"image/*" : @"video/*";
    }
    requestBody.fileName = fileName;
    requestBody.mimeType = mimeType;
    requestBody.fileData = fileData;
    requestBody.fileURL = fileURL;
    
    if (vcName) {
        requestBody.requestVCName = vcName;
        requestBody.isAutoCancelRequest = YES;
    }
    
    requestBody.uploadProgressBlock = uploadProgress;
    requestBody.successBlock = success;
    requestBody.failureBlock = failure;
    
    return [ACNetworkManager sendTaskWithRequest:requestBody];
}

#pragma mark - 基础

+ (NSURLSessionTask *)requestWithUrl: (NSString *)url
                          methodType: (ACMethodType)methodType
                         requestType: (ACRequestType)requestType
                              vcName: (NSString *)vcName
                        fileSavePath: (NSString *)fileSavePath
                     allHeaderFields: (NSDictionary *)allHeaderFields
                          parameters: (NSDictionary *)parameters
                            downloadProgress: (ACResponseProgressBlock)downloadProgress
                             success: (ACResponseSuccessBlock)success
                             failure: (ACResponseFailureBlock)failure
{
    //报文s传输格式：post/get 默认json、download/upload 默认text
    ACRequestBody *requestBody = [[ACRequestBody alloc] init];
    requestBody.methodType = methodType;
    requestBody.requestType = requestType;
    requestBody.parameters = parameters;
    requestBody.url = url;
    requestBody.allHeaderFields = allHeaderFields;
    
    requestBody.fileSavePath = fileSavePath;
    
    requestBody.downloadProgressBlock = downloadProgress;
    requestBody.successBlock = success;
    requestBody.failureBlock = failure;
    
    if (vcName) {
        requestBody.requestVCName = vcName;
        requestBody.isAutoCancelRequest = YES;
    }
    
    return [ACNetworkManager sendTaskWithRequest:requestBody];
}

@end
